package com.shunhe.storage.ui.contact;

import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.KeyEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import androidx.appcompat.widget.AppCompatEditText;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.ruitu.arad.Arad;
import com.ruitu.arad.base.BaseActivity;
import com.ruitu.arad.util.ToastUtils;
import com.ruitu.router_module.bean.EventModel;
import com.ruitu.router_module.support.view.LinearItemDecoration;
import com.shunhe.storage.R;
import com.shunhe.storage.dao.model.Customer;
import com.shunhe.storage.ui.adapter.SwipeCustomerListAdapter;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

// 客户列表
public class CustomerListActivity extends BaseActivity {
    private RecyclerView rcv_list;
    private TextView tv_add;// 添加客户
    private AppCompatEditText et_word;

    private SwipeCustomerListAdapter adapter;
    private List<Customer> customerList = new ArrayList<>();

    private ArrayList<Customer> spinnerCustomerList = new ArrayList<>();
    private CustomerSpinnerAdapter spinnerAdapter;

    @Override
    protected int getLayoutRes() {
        return R.layout.activity_customer_list;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        rcv_list = findViewById(R.id.rcv_list);
        tv_add = findViewById(R.id.tv_add);
        et_word = findViewById(R.id.et_word);

        setHeadTitle("客户管理");

        LinearLayoutManager manager = new LinearLayoutManager(this);
        adapter = new SwipeCustomerListAdapter(this);
        LinearItemDecoration divider = new LinearItemDecoration.Builder(this)
                .setSpan(R.dimen.deco_10_dp)
//                .setPadding(R.dimen.deco_10_dp)
//                .setLeftPadding(R.dimen.deco_10_dp)
//                .setRightPadding(R.dimen.deco_10_dp)
                .setColorResource(R.color.divider_list)
                .setShowLastLine(true)
                .build();
        rcv_list.addItemDecoration(divider);
        rcv_list.setLayoutManager(manager);
        rcv_list.setAdapter(adapter);
        for (int i = 0; i < 5; i++) {
            Customer customer = new Customer();
            customer.setName("客户" + i);
            customer.setPhone(13600006660L + i + "");
            customer.setAddress("临沂市兰山区临西十路" + (i + 10) + "号");
            customerList.add(customer);
        }
        adapter.setDataList(customerList);

        setListeners();

        Arad.bus.register(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (Arad.bus.isRegistered(this)) {
            Arad.bus.unregister(this);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventBus(EventModel ev) {
        if (ev.getEventCode() == 20121751) {// 添加客户完成
            Customer customer = (Customer) ev.getEventObj();
            customerList.add(customer);
            adapter.setDataList(customerList);
        }
    }

    private void setListeners() {
        setOnClickListener(tv_add);
        adapter.setOnSwipeListener(new SwipeCustomerListAdapter.OnSwipeListener() {
            @Override
            public void onDel(Object obj, int index) {
                Customer customer = (Customer) obj;
                ToastUtils.showShort("删除  index = " + index);
                customerList.remove(index);
                adapter.setDataList(customerList);
            }
        });

        et_word.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                String word = et_word.getText().toString().trim();
                if (TextUtils.isEmpty(word)) {
                    adapter.setDataList(customerList);
                } else {
                    queryCustomerByName(word);
                }
                return false;
            }
        });
    }

    // 通过客户名称筛选客户信息
    private void queryCustomerByName(String custName) {
        List<Customer> tempCustmerList = new ArrayList<>();
        for (int i = 0; i < customerList.size(); i++) {
            Customer customer = customerList.get(i);
            if (customer.getName().trim().contains(custName.trim())) {// 符合筛选条件
                tempCustmerList.add(customer);
            }
        }
        adapter.setDataList(tempCustmerList);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v == tv_add) {// 添加
//            ToastUtils.showShort("添加客户");
            startActivity(AddCustomerActivity.class);
        }
    }

    // 201216用不到了
    private class CustomerSpinnerAdapter extends BaseAdapter {
        private Context ctx;
        private ArrayList<Customer> dataList;

        public CustomerSpinnerAdapter(Context ctx, ArrayList<Customer> dataList) {
            this.ctx = ctx;
            this.dataList = dataList;
        }

        @Override
        public int getCount() {
            return dataList.size();
        }

        @Override
        public Customer getItem(int position) {
            return dataList.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = View.inflate(ctx, R.layout.item_goods_list_spinner, null);
                new CustomerSpinnerAdapter.ViewHolder(convertView);
            }
            CustomerSpinnerAdapter.ViewHolder holder = (CustomerSpinnerAdapter.ViewHolder) convertView.getTag();// get convertView's holder

            holder.car_brand.setText(getItem(position).getName());

            return convertView;
        }

        class ViewHolder {
            TextView car_brand;
//            TextView car_owner;
//            TextView car_cost;

            public ViewHolder(View convertView) {
                car_brand = convertView.findViewById(R.id.tv_name);
//                car_owner = (TextView) convertView.findViewById(R.id.tv_car_owner);
//                car_cost = (TextView) convertView.findViewById(R.id.tv_car_cost);
                convertView.setTag(this);//set a viewholder
            }
        }
    }
}